package com.hns.can.safety.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysFieldValueRegisterEntity;
import com.hns.can.common.aop.orgTree.DriverInterfaceAnnotation;
import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.aop.sedate.SedateAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.master.entity.CommDrvSctBhvInfoEntity;
import com.hns.can.safety.vo.DrivingBehaviorParamVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * 驾驶行为Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class DrivingBehaviorService extends CommonService {
	
    @Autowired
    private FillCodeDscValue codeDscValue;
    
    /**
     * 翻译解释基础名称
     * @param list
     * @author:zhangzq
     * @email:
     */
    private void fillCodeDscValue(List list) throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo1 = new FillParamsVo("lineId", map);
        map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo2 = new FillParamsVo("organId", map);
        map = new HashMap<String,String>();
        map.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo3 = new FillParamsVo("drvId", map);
        map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo4 = new FillParamsVo("carId", map);
        codeDscValue.fillInfoValue(list,new String[]{GlobalConst.CACHE_KEY_BASE_LINE_INFO,GlobalConst.CACHE_KEY_BASE_ORGAN,GlobalConst.CACHE_KEY_BASE_DRV_INFO,GlobalConst.CACHE_KEY_BASE_CAR_INFO},vo1,vo2,vo3,vo4);
        map = new HashMap<String,String>();
        map.put("drvSctBhv", "DRV00000");
        codeDscValue.fillCodeDscValue(list, map);
    }
    
    /**
     * 填充机构线路名称数据
     * @param list
     * @return
     * @author:zhangzq
     * @email:
     */
    private void fillOrganLineName(List list)throws Exception {
    	Map<String, String> map = new HashMap<String, String>();
        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo1 = new FillParamsVo("lineId", map);
        map = new HashMap<String, String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo2 = new FillParamsVo("organId", map);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_LINE_INFO, GlobalConst.CACHE_KEY_BASE_ORGAN}, vo1, vo2);
    }
    
    /**
     * 填充线路驾驶员姓名数据
     * @param list
     * @return
     * @author:zhangzq
     * @email:
     */
    private void fillLineDrvName(List list)throws Exception {
    	 Map<String,String> map = new HashMap<String,String>();
         map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
         FillParamsVo vo1 = new FillParamsVo("lineId", map);
         map = new HashMap<String,String>();
         map.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);
         FillParamsVo vo2 = new FillParamsVo("drvId", map);
         codeDscValue.fillInfoValue(list,new String[]{GlobalConst.CACHE_KEY_BASE_LINE_INFO,GlobalConst.CACHE_KEY_BASE_DRV_INFO},vo1,vo2);
    }
    
    /**
     * 分页查询驾驶员行为数据
     * @param entity
     * @return
     * @author:zhangzq
     * @email:
     */
	@SuppressWarnings({"rawtypes", "unchecked" })
	@LeftOrgAnnotation()
//	@SedateAnnotation()
	@Transactional
	@DriverInterfaceAnnotation()
	public Pagination searchDrivingBehavior(DrivingBehaviorParamVo entity)throws Exception{
		Pagination page = this.myBatisPage("drivingBehavior.getDrivingBehaviorListCount","drivingBehavior.getDrivingBehaviorList", entity,CommDrvSctBhvInfoEntity.class);
		List list=page.getData();
		fillCodeDscValue(list);
		page.setData(list);
		return page;
	}
	
	/**
	 * 查询驾驶员行为数据用于导出
	 * @param conditionVo
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@LeftOrgAnnotation()
    @Transactional
//    @SedateAnnotation()
    @DriverInterfaceAnnotation()
    public List<BaseEntity> getDrivingBehaviorList(DrivingBehaviorParamVo conditionVo) throws Exception {
        List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("drivingBehavior.getDrivingBehaviorList", conditionVo), CommDrvSctBhvInfoEntity.class);
        fillCodeDscValue(list);
        return list;
    }

    /**
     * 查询驾驶行为统计数据
     * @param conditionVo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @SuppressWarnings("unchecked")
    @LeftOrgAnnotation()
    @Transactional
//    @SedateAnnotation()
    @DriverInterfaceAnnotation()
    public Pagination searchDrivingBehaviorStatistics(DrivingBehaviorParamVo conditionVo) throws Exception {
        Pagination page = null;
        List list=null;
        if(conditionVo.getStatisType().equals("LINE")) {
            page = this.myBatisPage("drivingBehavior.getLineBehaviorStatisCount", "drivingBehavior.getLineBehaviorStatisList", conditionVo, CommDrvSctBhvInfoEntity.class);
            list=page.getData();
            fillOrganLineName(list);
        }
        else if(conditionVo.getStatisType().equals("CAR")){
            page = this.myBatisPage("drivingBehavior.getCarBehaviorStatisCount", "drivingBehavior.getCarBehaviorStatisList", conditionVo, CommDrvSctBhvInfoEntity.class);
            list=page.getData();
            fillCodeDscValue(list);
        }
        else if(conditionVo.getStatisType().equals("DRV")){
            page = this.myBatisPage("drivingBehavior.getDrivingBehaviorStatisticsCount", "drivingBehavior.getDrivingBehaviorStatisticsList", conditionVo, CommDrvSctBhvInfoEntity.class);
            list=page.getData();
            fillCodeDscValue(list);
        }
        page.setData(list);
        return page;
    }
	
    /**
     * 驾驶行为统计数据用于导出
     * @param conditionVo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @SuppressWarnings("unchecked")
	@LeftOrgAnnotation()
    @Transactional
    @SedateAnnotation()
    @DriverInterfaceAnnotation()
    public List<CommDrvSctBhvInfoEntity> getDrivingBehaviorStatistics(DrivingBehaviorParamVo conditionVo) throws Exception {
        List<CommDrvSctBhvInfoEntity> list = null;
        if(conditionVo.getStatisType().equals("LINE")) {
        	fillOrganLineName(list);
        }
        else{
        	fillLineDrvName(list);
        }
        if(conditionVo.getStatisType().equals("LINE")) {
            list =  PubUtil.convertListMapToObject(sqlMapDao.selectList("drivingBehavior.getLineBehaviorStatisList", conditionVo), CommDrvSctBhvInfoEntity.class);
            fillOrganLineName(list);
        }
        else if(conditionVo.getStatisType().equals("CAR")){
            list =  PubUtil.convertListMapToObject(sqlMapDao.selectList("drivingBehavior.getCarBehaviorStatisList", conditionVo), CommDrvSctBhvInfoEntity.class);
            fillCodeDscValue(list);
        }
        else if(conditionVo.getStatisType().equals("DRV")){
            list =  PubUtil.convertListMapToObject(sqlMapDao.selectList("drivingBehavior.getDrivingBehaviorStatisticsList", conditionVo), CommDrvSctBhvInfoEntity.class);
            fillCodeDscValue(list);
        }
        return list;
    }

    /**
     * 驾驶行为统计数据用于图表
     * @param conditionVo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @SuppressWarnings("unchecked")
    @LeftOrgAnnotation()
    @Transactional
    @SedateAnnotation()
    @DriverInterfaceAnnotation()
    public List<CommDrvSctBhvInfoEntity> getDrivingBehaviorStatisticsForChart(DrivingBehaviorParamVo conditionVo) throws Exception {
        List<CommDrvSctBhvInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("drivingBehavior.getDrivingBehaviorStatisticsForChart", conditionVo), CommDrvSctBhvInfoEntity.class);
        return list;
    }
    
    @Transactional
    public List<SysFieldValueRegisterEntity> getBhvDsc(){
    	Map map=new HashMap<String,String>();
    	map.put("userId", this.getCurrentUser().getUser().getId());
    	List<SysFieldValueRegisterEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("drivingBehavior.getBhvDscByUserId", map), SysFieldValueRegisterEntity.class);
        return list;
    }
}
